<?php
/**
 * Created by PhpStorm.
 * User: Kevin
 * Date: 2018/9/29
 * Time: 17:09
 */
namespace app\admin\controller;
use think\Db;
use app\admin\model\SalesmanModel;
class Salesman extends Base
{
    public function index(){
        if(request()->isAjax ()){
            extract(input());
            $map = [];
            if(isset($role)&&$role != ""){
                $map['user_job'] = $role;
            }
            if(isset($key)&&$key!="")
            {
                $map['user_phone|user_name|user_mynum'] = ['like',"%" . $key . "%"];
            }
            $Nowpage = input('page') ? input('page'):1;
            $limits = input("limit")?input("limit"):10;// 获取总条数;
            $user = new SalesmanModel();
            $lists = $user->getSalesmanData($map, $Nowpage, $limits);
            $count = $user->getSalesmanCount($map);
            return json(['code'=>0,'msg'=>'','count'=>$count,'data'=>$lists]);
        }
        return $this->fetch('salesman/index');
    }

    /*
     * 业务订单
     */
    public function order(){
        if(request()->isAjax ()){
            extract(input());
            $map = [];
            $map['o_level'] = 1;
            $map['o_status'] = 2;
            if(isset($key)&&$key!="")
            {
                $map['o_number'] = ['like',"%" . $key . "%"];
            }
            $Nowpage = input('page') ? input('page'):1;
            $limits = input("limit")?input("limit"):10;// 获取总条数;
            $lists = Db::name('work_order o')
                ->join('work_user u','o.o_user_id = u.user_id','left')
                ->join('shops s','o.o_user_id = s.s_id','left')
                ->where($map)
                ->field('o_id,o_number,o_agree,o_user_pay,o_status,o_level,o_money,o_datetime,o_type,u.user_name uname,o_music,s_name,o_admin')
                ->order('o_datetime desc')
                ->page($Nowpage,$limits)
                ->select();
            $count = Db::name('work_order o')
                ->join('work_user u','o.o_user_id = u.user_id','left')
                ->where($map)
                ->count();
            return json(['code'=>0,'msg'=>'','count'=>$count,'data'=>$lists]);
        }
        return $this->fetch('salesman/order');
    }

    /*
     * 商户入驻审核
     */
    public function agreeshop(){
        extract(input());
        $tui = new \app\work\controller\Index();
        if($type == 2){
            $result = $tui->agreeShopIn($id,session('username'));
        }else{
            $result = $tui->refuseShopIn($id,$value,session('username'));
        }
        if($result['status'] == 200){
            return json(['code'=>200,'msg'=>'操作成功']);
        }else{
            return json(['code'=>100,'msg'=>'操作失败，请联系管理员']);
        }
    }

    public function shopDetail(){
        $data = Db::name('work_shops_copy')
            ->where('s_ordernumber',input('id'))
            ->field('s_name,s_pinlei,s_image,s_door,s_food,s_shou,s_zheng,s_fan,s_inside,s_bianhao,s_beiyongphone,s_phone,s_address,s_join_in')
            ->find();
        return $this->fetch('salesman/order_detail',['data'=>$data]);
    }

    public function music(){
        extract(input());
        Db::name('work_order')->where('o_id',$id)->setField ($field,$value);
        return json(['code'=>'123456','msg'=>'123456']);
    }

    public function user_state()
    {
        extract(input());
        $role = new SalesmanModel();
        $flag = $role->memberState($id,$num);
        return json(['code' => $flag['code'], 'data' => $flag['data'], 'msg' => $flag['msg']]);
    }
}